package xyz.labmem.base.sys.module.config.controller


import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import xyz.labmem.base.core.annotion.BusinessLog
import xyz.labmem.base.core.base.BaseVerify
import xyz.labmem.base.core.consts.*
import xyz.labmem.base.core.enums.LogAnnotationOpTypeEnum
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.core.tool.isnull
import xyz.labmem.base.sys.base.BaseController
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.base.sys.module.config.entity.SysConfig
import xyz.labmem.base.sys.module.config.mapper.SysConfigMapper
import xyz.labmem.base.sys.module.config.service.SysConfigService


@Api(tags = ["时效设置"])
@RestController
@RequestMapping("${PathPrefixConst.SYS_PATH_PREFIX}config")
class SysConfigController(
    private var sysConfigMapper: SysConfigMapper,
    private var sysConfigService: SysConfigService
    )  : BaseController() {

    @PutMapping("saveConfig")
    @ApiOperation(value = "保存时效设置")
    @BusinessLog(title = "保存时效设置", opType = LogAnnotationOpTypeEnum.EDIT)
    fun saveConfig(
        @RequestBody @Validated(BaseVerify.put::class) req: SysConfig
    ): RV<*> {
        return if (req.id?.isnull() == true) {
            resultJudgment(sysConfigMapper.insert(req))
        } else
            resultJudgment(sysConfigMapper.updateById(req))
    }


    @GetMapping("queryConfig")
    @ApiOperation(value = "获取时效设置")
    fun queryConfig(): RV<*> {
        return success(sysConfigService.getConfigByCode(TIME_CONFIG_ID))
    }
}